package com.dc.bobaoge.project.v2.to.v3.business.core;

import com.dc.bobaoge.project.base.core.old.mapper.OldCoreTradeRecordMapper;
import com.own.business.pay.common.constant.ConstantPay;
import com.own.business.pay.common.em.PayEnum;
import com.own.business.pay.entity.po.BillHistory;
import com.own.business.pay.mapper.BillHistoryMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * TradeRecordResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-30 11:02:00
 */
@Slf4j
@Component
public class TradeRecordResolveMethod implements ResolveMethod {

    private static final Map<Integer, PayEnum.Method> PAY_METHOD_MAP = new HashMap<>() {{
        put(1, PayEnum.Method.WECHAT);
        put(2, PayEnum.Method.ALIPAY);
        put(3, PayEnum.Method.BALANCE);
    }};
    private static final Map<Integer, Integer> COST_MAP_MAP = new HashMap<>() {{
        put(1, 2);
        put(2, 1);
    }};

    private static final Map<Integer, String> TRADE_TYPE_MAP = new HashMap<>() {{
        put(1, "充值");
        put(2, "订阅");
        put(3, "发布消息");
        put(4, "拼手气");
        put(5, "打赏");
        put(6, "信息分享红包");
        put(7, "拼手气红包");
        put(8, "管理员红包");
        put(9, "拼手气虚拟商品");
        put(10, "用户已发布未通过消息退回");
        put(11, "用户已发布删除消息退回");
    }};

    @Resource
    private OldCoreTradeRecordMapper oldCoreTradeRecordMapper;
    @Resource
    private BillHistoryMapper billHistoryMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        throw new BusinessSimpleException("不支持的操作");
    }

    /**
     * 新增的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void add(SyncTrigger trigger) {
        var oldCoreTradeRecord = oldCoreTradeRecordMapper.selectById(trigger.getTargetId());
        if (oldCoreTradeRecord == null) {
            throw new BusinessSimpleException("交易记录不存在");
        }
        // 新增信息
        var bill = new BillHistory();
        bill.setFkUserId(oldCoreTradeRecord.getUserId());
        bill.setTradeType(PAY_METHOD_MAP.get(oldCoreTradeRecord.getPayType()).getType());
        bill.setType(COST_MAP_MAP.get(oldCoreTradeRecord.getCostType()));
        bill.setOutTradeNo(null);
        bill.setOutRefundNo(null);
        bill.setSubject(TRADE_TYPE_MAP.get(oldCoreTradeRecord.getTradeType()));
        bill.setBody(TRADE_TYPE_MAP.get(oldCoreTradeRecord.getTradeType()));
        bill.setTotalAmount(Long.parseLong(oldCoreTradeRecord.getTradeMoney()));
        bill.setRefundAmount(0L);
        bill.setExpireTime(null);
        bill.setStatus(ConstantPay.Status.COMPLETE_SUCCESS);
        bill.setTradeTime(oldCoreTradeRecord.getTradeTime());
        bill.setNotifyData("");
        billHistoryMapper.insert(bill);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "trade_record";
    }

}
